package edu.office.applicationRepository;


import edu.office.entities.*;
import edu.office.tools.EOMessage;
import edu.office.tools.EOMessage1;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Named;

import java.time.LocalDate;
import java.util.Optional;

@Named("praiseRepositoryA")
@ApplicationScoped
public class PraiseRepository extends ApplicationCommonRepository<Praise> {


    //学生点赞  false:吐槽  true:点赞
    public String praise(Object obj, boolean flag, Student stu) {
        int praise = 0;
        if (flag) {
            praise = 1;
        } else {
            praise = -1;
        }
        Knowledge k = null;
        Learningresource l = null;
        Question q = null;
        //判断资源类型并写入新的赞个数
        if (obj instanceof Knowledge) {
            k = (Knowledge) obj;
            k.setPRAISE_CNT(Optional.ofNullable(k.getPRAISE_CNT()).orElse(0) + praise);
        } else if (obj instanceof Learningresource) {
            l = (Learningresource) obj;
            l.setPRAISE_CNT(Optional.ofNullable(l.getPRAISE_CNT()).orElse(0) + praise);
        } else if (obj instanceof Question) {
            q = (Question) obj;
            q.setPRAISE_CNT(Optional.ofNullable(q.getPRAISE_CNT()).orElse(0) + praise);
        }
        Praise p = new Praise();
        if (k != null) {
            p.setKNOWLEDGE_ID(k.getID());
        }
        if (l != null) {
            p.setLEARNINGRESOURCE_ID(l.getID());
        }
        if (q != null) {
            p.setQUESTION_ID(q.getID());
        }
        p.setPRAISE_DATE(LocalDate.now());
        p.setSTUDENT_ID(stu.getId());
        create(p);
        //数据写入数据库后使得当前学生点赞数加一
        return EOMessage.getEOMessageENUM(EOMessage1.Succeed);
    }
}
